package org.admanager.patternview.plugin.diagram.part;

import org.admanager.patternview.plugin.diagram.edit.parts.Component2EditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentComponentCompartment2EditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentComponentCompartmentEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentName2EditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentNameEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentReference2EditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentReferenceEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentService2EditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ComponentServiceEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.CompositeCompositeCompartmentEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.CompositeEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.CompositeNameEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.PatternViewEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ReferenceEditPart;
import org.admanager.patternview.plugin.diagram.edit.parts.ServiceEditPart;
import org.admanager.patternview.plugin.patternview.PatternView;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class PatternViewVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "org.admanager.patternview.plugin.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (PatternViewEditPart.MODEL_ID.equals(view.getType())) {
				return PatternViewEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return org.admanager.patternview.plugin.diagram.part.PatternViewVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				PatternViewDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
				.getPatternView().isSuperTypeOf(domainElement.eClass())
				&& isDiagram((PatternView) domainElement)) {
			return PatternViewEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = org.admanager.patternview.plugin.diagram.part.PatternViewVisualIDRegistry
				.getModelID(containerView);
		if (!PatternViewEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (PatternViewEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.admanager.patternview.plugin.diagram.part.PatternViewVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = PatternViewEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case PatternViewEditPart.VISUAL_ID:
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComposite().isSuperTypeOf(domainElement.eClass())) {
				return CompositeEditPart.VISUAL_ID;
			}
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponent().isSuperTypeOf(domainElement.eClass())) {
				return Component2EditPart.VISUAL_ID;
			}
			break;
		case CompositeCompositeCompartmentEditPart.VISUAL_ID:
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponent().isSuperTypeOf(domainElement.eClass())) {
				return ComponentEditPart.VISUAL_ID;
			}
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getReference().isSuperTypeOf(domainElement.eClass())) {
				return ReferenceEditPart.VISUAL_ID;
			}
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getService().isSuperTypeOf(domainElement.eClass())) {
				return ServiceEditPart.VISUAL_ID;
			}
			break;
		case ComponentComponentCompartmentEditPart.VISUAL_ID:
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponentReference().isSuperTypeOf(
							domainElement.eClass())) {
				return ComponentReferenceEditPart.VISUAL_ID;
			}
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponentService()
					.isSuperTypeOf(domainElement.eClass())) {
				return ComponentServiceEditPart.VISUAL_ID;
			}
			break;
		case ComponentComponentCompartment2EditPart.VISUAL_ID:
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponentReference().isSuperTypeOf(
							domainElement.eClass())) {
				return ComponentReference2EditPart.VISUAL_ID;
			}
			if (org.admanager.patternview.plugin.patternview.patternviewPackage.eINSTANCE
					.getComponentService()
					.isSuperTypeOf(domainElement.eClass())) {
				return ComponentService2EditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = org.admanager.patternview.plugin.diagram.part.PatternViewVisualIDRegistry
				.getModelID(containerView);
		if (!PatternViewEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (PatternViewEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.admanager.patternview.plugin.diagram.part.PatternViewVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = PatternViewEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case PatternViewEditPart.VISUAL_ID:
			if (CompositeEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Component2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeEditPart.VISUAL_ID:
			if (CompositeNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (CompositeCompositeCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Component2EditPart.VISUAL_ID:
			if (ComponentName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentComponentCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentEditPart.VISUAL_ID:
			if (ComponentNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentComponentCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case CompositeCompositeCompartmentEditPart.VISUAL_ID:
			if (ComponentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ReferenceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ServiceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentComponentCompartmentEditPart.VISUAL_ID:
			if (ComponentReferenceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentServiceEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentComponentCompartment2EditPart.VISUAL_ID:
			if (ComponentReference2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentService2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(PatternView element) {
		return true;
	}

}
